package com.culture.config.core;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.culture.config.common.util.ExceptionUtil;
import com.culture.config.common.util.ResourceUtil;
import com.culture.config.util.AuthenticationUtil;

import lombok.extern.slf4j.Slf4j;
import springfox.documentation.annotations.ApiIgnore;

@Slf4j
@ApiIgnore
@Controller
@RequestMapping({"${springfox.documentation.swagger-ui.base-url:}/swagger-ui"})
public class Swagger3UIController {
	private static final String RESOURCE_PATH = "/META-INF/resources/webjars/springfox-swagger-ui/";
	
	@GetMapping("/{fileName:.*}")
	public void index(@PathVariable(value="fileName",required=false)String fileName,
			HttpServletRequest request,HttpServletResponse reponse) {
		log.info(fileName);
		ExceptionUtil.businessExceptionWhenTrue(StringUtils.isBlank(fileName), "Bad Reqeust.");
		if(StringUtils.startsWith(fileName, "index.html")) {
			fileName = "index.html";
			AuthenticationUtil.validateCrsfToken(request);
		}
		ResourceUtil.writeClassResourceFile(reponse,RESOURCE_PATH.concat(fileName));		
	}
	
}
